/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.bitmap
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class BitmapModificatory
	{
		public static function BitmapResize(bitmap:Bitmap, size:Point, centre:Boolean = false):Bitmap
		{
			var result:Bitmap;
			var resultBitmapData:BitmapData;
			
			var dz:Number;
			var wx:Number = size.x / bitmap.width;
			var hy:Number = size.y / bitmap.height;
			
			if(wx > hy){
				dz = hy;
			}else{
				dz = wx;
			}
			
			if(dz <= 1){
				var bitmapSize:Number;
				if(bitmap.width <= bitmap.height){
					bitmapSize = bitmap.width;
				}else{
					bitmapSize = bitmap.height;
				}
				resultBitmapData = new BitmapData(bitmapSize, bitmapSize);				
				var rectangel:Rectangle = new Rectangle(0,0,bitmapSize, bitmapSize);
				resultBitmapData.copyPixels(bitmap.bitmapData, rectangel, new Point());
				result = new Bitmap(resultBitmapData);
				result.width *= dz;
				result.height *= dz;
			}else{
				result = new Bitmap(bitmap.bitmapData);
				result.width /= dz;
				result.height /= dz;
			}
			
			if(centre){
				result.x = size.x/2 - result.width/2;
				result.y = size.y/2 - result.height/2;
				
			}
			return result;
		}
	}
}